<?php

/**
 * TemplateCategoryController is the default controller for this application
 */
class TemplateCategoryController extends ControllerBase {
	/**
	 * TemplateCategoryDataService Object
	 *
	 * @var TemplateCategoryDataService object
	 */
	public $templateCategoryDataService = null;
	
	public $masterId = null;
	
	public function __construct() {
		parent::__construct ();
		
		// Set page name
		$this->setPageName ( 'templates' );
		
		// Init Authentication class
		$authentication = new Authentication ();
		
		// Only administrator can right
		if ($authentication->isAdministrator () == false) {
			
			// Go to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Login/index';
			CommonUtils::redirect ( $url );
		}
		
		// Initialize TemplateCategoryDataService
		$this->templateCategoryDataService = new TemplateCategoryDataService ();
	
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site. Return list of all product category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function indexAction() {
		// View product_category list
		$this->listAction ();
	}
	
	/**
	 * Dispplay List of product_category
	 * landing page of the site. Return list of all product_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function listAction() {
		$arrElement = array ();
		$itemList = $this->templateCategoryDataService->getItems ( $arrElement );
		
		$this->smarty->assign ( 'itemList', $itemList );
		$this->smarty->assign ( 'this', $this );
		
		$_SESSION ['tokenKey'] = sha1 ( CommonUtils::randomNumber ( 10 ) );
		$this->smarty->assign ( 'actionKey', $_SESSION ['tokenKey'] );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/TemplateCategory/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Add action is trigger when admin want to add the product_category information
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function addAction() {
		
		// Get action key
		$actionKey = $this->getPost ( 'actionKey', '' );
		$isError = false;
		$error = "";
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		if (! empty ( $actionForm ) && $actionForm == 'submit' && (isset ( $_SESSION ['tokenKey'] ) && $actionKey == $_SESSION ['tokenKey'])) {
			
			// Get id field
			$id = $this->getPost ( 'id', '' );
			
			// Get name field
			$name = $this->getPost ( 'name', '' );
			
			// Get description field
			$description = $this->getPost ( 'description', '' );
			
			try {
				// Save data into database
				$templateCategoryEntity = $this->templateCategoryDataService->beforeCreate ();
				$templateCategoryEntity->name = $name;
				$templateCategoryEntity->description = $description;
				$res = $this->templateCategoryDataService->create ( $templateCategoryEntity );
				
				$this->smarty->assign ( 'name', isset ( $isError ) ? $name : $templateCategoryEntity->name );
				$this->smarty->assign ( 'description', isset ( $isError ) ? $description : $templateCategoryEntity->description );
				
				// Show a message
				$error = 'The item has been created successfully';
			} catch ( Exception $ex ) {
				$isError = true;
				// Ignore to process this case. Only show a message
				$error = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
			}
		}
		
		// Create new token key
		$_SESSION ['tokenKey'] = sha1 ( CommonUtils::randomNumber ( 10 ) );
		$this->smarty->assign ( 'error', isset ( $error ) ? $error : '' );
		$this->smarty->assign ( 'isError', isset ( $isError ) ? $isError : false );
		$this->smarty->assign ( 'actionKey', $_SESSION ['tokenKey'] );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/TemplateCategory/add.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Edit action is trigger when admin want to edit the product_category information
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function editAction() {
		// Error flag
		$isError = false;
		$error = "";
		
		// Get id of product_category
		$id = $this->getPost ( 'id', '' );
		if (is_null ( $id ) || $id == '') {
			$id = $this->getQuery ( 'id', '' );
		}
		// If the id of product_category does not exist, go on to view list page
		if (empty ( $id )) {
			$this->listAction ();
			return - 1;
		}
		
		// Get action key
		$actionKey = $this->getPost ( 'actionKey', '' );
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		if (! empty ( $actionForm ) && $actionForm == 'submit' && (isset ( $_SESSION ['tokenKey'] ) && $actionKey == $_SESSION ['tokenKey'])) {
			
			// Get id field
			$id = $this->getPost ( 'id', '' );
			
			// Get name field
			$name = $this->getPost ( 'name', '' );
			
			// Get description field
			$description = $this->getPost ( 'description', '' );
			
			try {
				// Save data into database
				$templateCategoryEntity = $this->templateCategoryDataService->beforeUpdate ( $id );
				if (is_null ( $templateCategoryEntity )) {
					throw new Exception ( 'The id of product_category does not exist', 1 );
				}
				$templateCategoryEntity->id = $id;
				$templateCategoryEntity->name = $name;
				$templateCategoryEntity->description = $description;
				$res = $this->templateCategoryDataService->update ( $templateCategoryEntity );
				
				// Show a message
				$error = 'The record has been updated successfully';
			} catch ( Exception $ex ) {
				
				// Ignore to process this case. Only show a message
				$error = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
				$isError = true;
			}
		}
		
		// View data
		$templateCategoryEntityDetail = $this->templateCategoryDataService->getById ( $id );
		
		// If the id of product_category not exist, go on to view list page
		if (is_null ( $templateCategoryEntityDetail ) || is_null ( $templateCategoryEntityDetail )) {
			$this->listAction ();
			return - 1;
		}
		
		// Error Handling
		$this->smarty->assign ( 'isError', $isError );
		
		// Create new token key
		$_SESSION ['tokenKey'] = sha1 ( CommonUtils::randomNumber ( 10 ) );
		$this->smarty->assign ( 'error', isset ( $error ) ? $error : '' );
		$this->smarty->assign ( 'actionKey', $_SESSION ['tokenKey'] );
		$this->smarty->assign ( 'id', ($isError == true) ? $id : $templateCategoryEntityDetail->id );
		$this->smarty->assign ( 'name', ($isError == true) ? $name : $templateCategoryEntityDetail->name );
		$this->smarty->assign ( 'description', ($isError == true) ? $description : $templateCategoryEntityDetail->description );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/TemplateCategory/edit.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Active some records of product_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function activeAction() {
		$this->updateStatus ( TemplateCategoryEntity::ACTIVE );
		
		// View product_category list
		$this->listAction ();
	}
	
	/**
	 * Inactive some records of product_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function inactiveAction() {
		$this->updateStatus ( TemplateCategoryEntity::INACTIVE );
		
		// View product_category list
		$this->listAction ();
	}
	
	/**
	 * Delete action is trigger when administrator want to delete a product_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteAction() {
		// Get action key
		$actionKey = $this->getPost ( 'actionKey', '' );
		
		// Get Id of product_category
		$id = $this->getPost ( 'idValue', '' );
		
		// Delete a product_category item
		$res = $this->templateCategoryDataService->del ( $id );
		
		// View product_category list
		$this->listAction ();
	}
	
	/**
	 * Delete list of product_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteallAction() {
		$iCount = $this->getPost ( 'iCount', 0 );
		for($i = 0; $i < $iCount; $i ++) {
			$id = $this->getPost ( 'checkItem' . $i, '' );
			if (! empty ( $id )) {
				
				// Delete a product_category item
				$this->templateCategoryDataService->del ( $id );
			}
		}
		
		// View product_category list
		$this->listAction ();
	}
	
	/**
	 * Update status list of product_category
	 *
	 * @access private
	 *
	 * @param int $status: Status of the product_category
	 *
	 * @return void
	 */
	protected function updateStatus($status) {
		// Get action key
		$actionKey = $this->getPost ( 'actionKey', '' );
		if (isset ( $_SESSION ['tokenKey'] ) && $actionKey == $_SESSION ['tokenKey']) {
			$iCount = $this->getPost ( 'iCount', 0 );
			for($i = 0; $i < $iCount; $i ++) {
				$id = $this->getPost ( 'checkItem' . $i, '' );
				if (! empty ( $id )) {
					$this->templateCategoryDataService->updateStatus ( $id, $status );
				}
			}
		}
	}
}
